﻿//全局变量

//人员信息   
function fn_ownerTreeGrid() {
    $('#ownerTreeGrid').treegrid({
        width: 1490,
        fitColumns: true,
        rownumbers: true,
        url: '../Controller.aspx?controller=OwnerController&method=GetOwnerList',
        singleSelect: true,
        toolbar: [{
            id: 'btnreload-detail',
            text: '刷新',
            iconCls: 'icon-reload',
            handler: function() {
                 $('#ownerTreeGrid').treegrid('reload');  
            }
        },{
            id: 'btnsearch-detail',
            text: '查找',
            iconCls: 'icon-search',
            handler: function() {
                $('#dialog-search').dialog({ title: '查找人员' });
                $("#searchform").form('clear');
                $('#dialog-search').dialog('open');
            }
        },{
            id: 'btnadd-detail',
            text: '添加',
            iconCls: 'icon-add',
            handler: function() {
                $('#dialog-client').dialog({ title: '添加人员' });
                $("#clientform").form('clear');
                $('#dialog-client').dialog('open');
            }
        },{
            id: 'btnedit-detail',
            text: '修改',
            iconCls: 'icon-edit',
            handler: function() {
                $('#dialog-client').dialog({ title: '修改人员' });
                $("#clientform").form('clear');
                $('#dialog-client').dialog('open');
            }
        },{
            id: 'btndel-detail',
            text: '删除',
            iconCls: 'icon-no',
            handler: function() {
                   var node = $('#ownerTreeGrid').treegrid('getSelected');
			   if(node==null)
                {
                  alert('请您选择要删除的记录！');
                  return;
                }
                if (!confirm("确定要删除吗？"))
                  return;         
                var data = {OwnerId: node.OwnerId};
                    requestAjax("../Controller.aspx?controller=OwnerController&method=DelPerson",data, function(){
                        $('#ownerTreeGrid').treegrid('reload');  
                  });     
             }
          },{
            id: 'btnlook-detail',
            text: '查看',
            iconCls: 'icon-reload',
            handler: function() {
                //查看人员相关的房间、门面、停车位等
            }
        }],
            onDblClickRow: function() {
            }
        });
    }
 
 //查找人员
 function fn_dialog_searchperson() {
    $('#dialog-search').dialog({
        modal: true,
        inline: true,
        buttons: [{
            text: '查找',
            iconCls: 'icon-search',
            handler: function() {
               var PersonName = $('#searchname').val();
              var Cardnum = $('#searchIdcard').val();
              var Mobile = $('#searchMobile').val();
              $('#ownerTreeGrid').treegrid('options').url =   '../Controller.aspx?controller=OwnerController&method=GetOwnerList&PersonName='+PersonName+'&Cardnum='+Cardnum+'&Mobile='+Mobile;
              $('#ownerTreeGrid').treegrid('reload');
               $('#dialog-search').dialog('close');
            }
         }, {
            text: '取消',
            handler: function() {
                $('#dialog-search').dialog('close');
            }
        }]
     });
     $('#dialog-search').dialog('close');
    }
   
  //业主房间   
function fn_ownerRoomGrid() {
    $('#ownerRoomGrid').datagrid({
        rownumbers: true,
        singleSelect: true,
        onClickRow: function(rowIndex, rowData) {
              $("#clientform").form("load", rowData);
               var personid=rowData.PersonId;
               $("#OwnerId").val(personid);
               $('#formaddtype').val(0);
               
               $('#RelationType').combobox('setValue','21');
               $('#RelationType').combobox('disable');
               if(personid=="")
               {
                   $('#frompersex').combobox('clear');
                   $('#frompercardtype').combobox('clear');
               }
               $('#clientGrid').datagrid('options').url = '../Controller.aspx?controller=OwnerController&method=GetOwnerRelPer&personid='+personid;
               $('#clientGrid').datagrid('reload');
               
            }
        });
    }  
   
     //门面
function fn_shopGrid() {
    $('#shoplistGrid').datagrid({
        rownumbers: true,
        singleSelect: true,
        onClickRow: function(rowIndex, rowData) {
              $("#clientform").form("load", rowData);
               var personid=rowData.PersonId;
               $("#OwnerId").val(personid);
               $('#formaddtype').val(0);
               
               $('#RelationType').combobox('setValue','21');
               $('#RelationType').combobox('disable');
               if(personid=="")
               {
                   $('#frompersex').combobox('clear');
                   $('#frompercardtype').combobox('clear');
               }
               $('#clientGrid').datagrid('options').url = '../Controller.aspx?controller=OwnerController&method=GetOwnerRelPer&personid='+personid;
               $('#clientGrid').datagrid('reload');
               
            }
        });
    }  
    
    
   //人员房间   
function fn_clientGrid() {
    $('#clientGrid').datagrid({
        rownumbers: true,
        singleSelect: true,
        onDblClickRow: function(rowIndex, rowData) {
               $("#clientform").form("load", rowData);
               $("#frompermemo").val(rowData.PerMemo);
               $('#RelationType').combobox('enable');
               $('#RelationType').combobox('options').url="../Controller.aspx?controller=RoomController&method=GetBaseData&type=6";
               $('#RelationType').combobox('reload');
            }
        });
    }  
        
        
//新增客户弹出框   
function fn_dialog_client() {  
    $('#dialog-client').dialog({
        modal: true,
        inline: true,
        buttons: [{
            text: '保存',
            iconCls: 'icon-ok',
            handler: function() {
                  var roomid= $('#RoomId').val();
                  var pp = $('#tabRoomShop').tabs('getSelected');
                  var title = pp.panel('options').title; 
                  if(title=="房间")
                  { 
                       if(roomid==null)
                       {
                         alert("请选中对应的房间信息！");
                         return;
                       }
                        $('#formedittype').val(0);
                  }
                  if(title=="门面")
                  {
                       if(roomid==null)
                       {
                         alert("请选中对应的门面信息！");
                         return;
                       }
                        $('#formedittype').val(1);
                  }
                  
                    FormSubmit($('#clientform'), '../Controller.aspx?controller=OwnerController&method=AddPerson', function() {
                          if(title=="房间")
                          { 
                               $('#clientGrid').datagrid('reload');
                               $('#ownerRoomGrid').datagrid('reload');
                               $('#ownerRoomGrid').datagrid('selectRecord',roomid);
                          }
                          if(title=="门面")
                          {
                               $('#clientGrid').datagrid('reload');
                               $('#shoplistGrid').datagrid('reload');
                               $('#shoplistGrid').datagrid('selectRecord',roomid);
                          }
                       
                    });
            }
         },{
            text: '取消',
            handler: function() {
                $('#dialog-client').dialog('close');
            }
        }]
     });
     $('#dialog-client').dialog('close');
    }
    

 function loadData(){
		$('#searchArea').combobox({
			onSelect: function(record) {
			  var s=record.AreaCode;
               $('#searchBuilding').combobox('options').url = '../Controller.aspx?controller=RoomController&method=GetBuildingFieldList&AreaCode=' + s;
               $('#searchBuilding').combobox('reload');
               
           }
		});
	}
function fn_searchroom()
{
      var searchArea = $('#searchArea').combobox('getValue');
      var searchBuilding = $('#searchBuilding').combobox('getValue');
      var useType = $('#useType').combobox('getValue');
      var searchRoom = $('#searchroomId').val();
      $('#ownerRoomGrid').datagrid('options').queryParams={AreaCode:searchArea,BuildingCode:searchBuilding,UseType:useType,RoomNumber:searchRoom};
      $('#ownerRoomGrid').datagrid('options').url = '../Controller.aspx?controller=RoomController&method=GetRoomList';
      $('#ownerRoomGrid').datagrid('reload');
}

function fn_searchshop()
{
      var shopname = $('#txtshop').val();
      $('#shoplistGrid').datagrid('options').queryParams={shopname:shopname};
      $('#shoplistGrid').datagrid('options').url = '../Controller.aspx?controller=OwnerController&method=GetShopList';
      $('#shoplistGrid').datagrid('reload');
}

function fn_reset()
{
      var searchArea = $('#searchArea').combobox('clear');
      var searchBuilding = $('#searchBuilding').combobox('clear');
      var useType = $('#useType').combobox('clear');
      var searchRoom = $('#searchroomId').val("");
}

function fn_addownerfield()
{
      $("#clientform").form('clear');
      var selected=null;
      var pp = $('#tabRoomShop').tabs('getSelected');
      var title = pp.panel('options').title; 
      if(title=="房间")
      {
        selected = $('#ownerRoomGrid').datagrid('getSelected');
      }
      if(title=="门面")
      {
        selected = $('#shoplistGrid').datagrid('getSelected');
      }
      if(selected!=null)
      {
          var personid= selected.PersonId;
          if(personid==null || personid=="")
          {
            alert("请先维护业主！");
            return;
          }
           $('#POwnerId').val(selected.PersonId);
           $('#RoomId').val(selected.RoomId);
           $('#formaddtype').val(1);
           $('#RelationType').combobox('enable');
           $('#RelationType').combobox('options').url="../Controller.aspx?controller=RoomController&method=GetBaseData&type=6";
           $('#RelationType').combobox('reload');
      }
      else
      {
        alert("请先选择对应的业主信息！");
      }
}
	
function fn_delownwerfield()
{
       var node = $('#clientGrid').datagrid('getSelected');
	   if(node==null)
        {
          alert('请您选择要删除的记录！');
          return;
        }
        if (!confirm("确定要删除吗？"))
          return;         
        var data = {OwnerId: node.OwnerId};
            requestAjax("../Controller.aspx?controller=OwnerController&method=DelPerson",data, function(){
                $('#clientGrid').datagrid('reload');  
          });     
}	

function sexFormatter(value){ 
  if(value==1)
    return "男";
  else if(value==2)
    return "女";
  else
    return "未知性别";
}

function  typeFormatter(value){
    var typedata=$('#RelationType').combobox('getData');
	for(var i=0; i<typedata.length; i++){
		if (typedata[i].Code == value) return typedata[i].Name;
	}
	return value;
}

function  cardFormatter(value){
    var  carddata=$('#frompercardtype').combobox('getData');
	for(var i=0; i<carddata.length; i++){
		if (carddata[i].Code == value) return carddata[i].Name;
	}
	return value;
}

 //3.初始化入口
    $(function() {
       fn_dialog_searchperson();
       fn_ownerTreeGrid();
       fn_clientGrid();
       fn_ownerRoomGrid();
       fn_shopGrid();
       fn_dialog_client(); 
       loadData();      
    });
    